/* The file is part of the Waff web framework.
 *
 * Copyright (c) 2011, Grzegorz Skorupa
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditionsare met:
 *
 * - Redistributions of source code must retain the above copyright notice, this list of conditions
 *   and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions
 *   and the following disclaimer in the documentation and/or other materials provided with the distribution.
 * - Neither the name of the Grzegorz Skorupa nor the names of its contributors may be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package pl.org.waff.repository;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.KeyFactory;
import java.util.logging.Logger;

/**
 *
 * @author greg
 */
public class Folder {

    private String id = "new";
    private String path = "";
    private String parent = "";
    private String name = "";
    private String description = "";
    private static final Logger log = Logger.getLogger(Folder.class.getName());

    public Folder(Entity entity) {
        id = KeyFactory.keyToString(entity.getKey());
        description = ((com.google.appengine.api.datastore.Text) entity.getProperty("description")).getValue();
        path = (String) entity.getProperty("path");
        parent = (String) entity.getProperty("parent");
        name = (String) entity.getProperty("name");
    }

    public Folder() {
        id = "new";
        path = "";
        parent = "";
        name = "";
        description = "";
    }

    public Entity toEntity() {
        Entity entity;
        if ("new".equals(id) || "-1".equals(id)) {
            entity = new Entity("folder");
        } else {
            entity = new Entity(KeyFactory.stringToKey(id));
        }
        entity.setProperty("path", path);
        entity.setProperty("parent", parent);
        entity.setProperty("name", name);
        entity.setProperty("description", new com.google.appengine.api.datastore.Text(getDescription()));
        return entity;
    }

    public void save() {
        if (path == null || path.isEmpty()) {
            if ("/".equals(parent)) {
                path = parent + name;
            } else {
                path = parent + "/" + name;
            }
        }
    }

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * @return the title
     */
    public String getPath() {
        return path;
    }

    /**
     * @param title the title to set
     */
    public void setPath(String path) {
        this.path = path;
    }

    /**
     * @return the description
     */
    public String getDescription() {
        return description;
    }

    /**
     * @param description the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @return the parent
     */
    public String getParent() {
        return parent;
    }

    /**
     * @param parent the parent to set
     */
    public void setParent(String parent) {
        this.parent = parent;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
}
